package 算法.练习1;

import java.util.HashSet;

/**
 * @author:谢君臣
 * @Date:2021/7/20-15:16
 * @version:1.0
 * @function:
 */
public class NC41 {
    public static void main(String[] args) {
        System.out.println(maxLength(new int[]{1, 2, 3, 1, 2, 3, 2, 2}));
    }
    public static int maxLength(int[] arr) {
        // write code here
        int i = 0, j = 1, max = 0;
        HashSet<Integer> hashSet = new HashSet<>();
        if (arr.length<=1)
            return arr.length;
        hashSet.add(arr[0]);
        while (j<arr.length){
            while (hashSet.contains(arr[j]))hashSet.remove(arr[i++]);
            hashSet.add(arr[j]);
            max = Math.max(max,j-i+1);
            j++;
        }
        return max;
    }
}
